# -*- coding: UTF-8 -*-

import json

path = "usagov_bitly_data2012-03-16-1331923249.txt"

# 读文件 path 的一行，到 \n 结束
line = open(path).readline()

# 读文件 path 的所有行
lines = open(path).readlines()

print line

# 打印 path 文件的所有行
# print lines

records = [json.loads(line) for line in open(path)]

# 使用索引打印records list的前10条
for i in range(10, 20, 2):
    print records[i]

# 打印列表中的元素个数（长度）
print "list length = " + str(records.__len__())
print "the last list ele = " + str(records[records.__len__() - 1])

list.append(records, "test 123")

print "list length (after) = " + str(records.__len__())
print "the last list ele (after) = " + str(records[records.__len__() - 1])

# python中的集合对象
# 列表（list）：e.g. list1 = ['physics', 'chemistry', 1997, 2000]
# 元组（tuple）：e.g. tup1 = ('physics', 'chemistry', 1997, 2000)
# 字典（dictionary）： e.g. d = { key1 : value1, key2 : value2 }

print "\n===========================================================================\n"

re = records

# re list中取出tz字段的值
print re[0]['tz']

# 一个需求：得知上网数据集中在哪个时区（通过tz进行统计）

# 取出一组时区
time_zones = [rec['tz'] for rec in re if 'tz' in rec]

print time_zones

# 定义计数的方法
def get_counts(sequence):
    # 初始化技术的字典（map）
    counts = {}
    # 在序列中遍历
    for x in sequence:
        # 如果遍历得到的值在counts中存在则++
        if x in counts:
            counts[x] += 1
        # 否则就初始化为1
        else:
            counts[x] = 1
    return counts

counts = get_counts(time_zones)

# 使用标准库的方式实现计数方法
from collections import defaultdict

def get_count2(sequence):

    counts = defaultdict(int)
    for x in sequence:
        counts[x] += 1
    return counts

counts2 = get_count2(time_zones)

print len(counts)
print len(counts2)

print "\n===========================================================================\n"

# 获得特定顺序的排名

def top_counts(count_dict, n = 10):
    value_key_pairs = [(count, tz) for tz, count in count_dict.items()]
    value_key_pairs.sort()
    return value_key_pairs[-n:]

top_counts = top_counts(get_count2(time_zones))

# 使用collection.Counter简化逻辑

from collections import Counter

top_counts2 = Counter(time_zones)

top_counts2 = top_counts2.most_common(10)

print "\n\nend"



